import time
import unittest
from common.functions import browser_operation, backhost
from page.reception.goods.select_goods_page import SelectPage
from page.backstage.backstage_page import Backstage
from page.reception.goods.add_goods_page import AddGoodsPage

# 准备管理员账号密码
userinfo = {
    'username': 'admin',
    'password': 'admin123'
}


class UpdateTestCase(unittest.TestCase):
    # 大夹子
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = browser_operation()
        # 创建UpdatePage对象
        cls.select = SelectPage(driver)
        # 创建Backstage对象
        cls.backstage = Backstage(driver)
        # 创建AddGoodsPage对象
        cls.add = AddGoodsPage(driver)
        # 请求目标网址
        cls.select.get_website(backhost)

    # 产看商品操作
    def test_01_select_goods(self):
        # 后台登录操作
        self.backstage.implicitly_wait()
        # 点击xx
        self.backstage.click_X()
        # 点击logo
        self.backstage.backstage_login()
        # 账号密码输入
        self.backstage.input_username(userinfo['username'])
        # 密码输入
        self.backstage.input_password(userinfo['password'])
        # 点击登录按钮
        self.backstage.btn_login()

        # 切换菜单栏frame
        self.backstage.menu_frame()
        # 点击商品管理
        self.add.g_management()
        # 返回上一级frame
        self.add.change_parent_frame()
        # 切换main_frame
        self.add.min_frame()

        # 点击查看按钮
        self.select.select_goods()

        # 获取所有窗口句柄(driver.window_handles[])
        news_handle = self.select.driver.window_handles[1]
        # 切换标签页(driver.switch_to.window())
        self.select.driver.switch_to.window(news_handle)

        time.sleep(3)
        # 获取页面title
        title_name = self.select.title()
        print(title_name)

        # 断言
        self.assertNotEqual('ECSHOP 管理中心', title_name)

    # 后大夹子
    @classmethod
    def tearDownClass(cls) -> None:
        # 退出浏览器
        cls.select.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
